def connect_db():
    """创建数据库连接（使用现有products表）"""
    import os
    db_path = os.path.join(os.path.dirname(__file__), "trade_monitor.db")
    
    conn = None
    try:
        conn = sqlite3.connect(db_path)
        # 使用现有表，无需创建新表
        return conn
    except Error as e:
        print(f"数据库连接错误: {e}")
        return None

def insert_main_contract(conn, contract):
    """添加主力合约到products表"""
    sql = '''INSERT INTO products(name, exchange_code, margin, stoploss, takeprofit, is_monitored)
             VALUES(?,?,?,?,?,?)'''
    cur = conn.cursor()
    try:
        cur.execute(sql, contract)
        conn.commit()
        return cur.lastrowid
    except Error as e:
        print(f"添加主力合约错误: {e}")
        return None

if __name__ == '__main__':
    conn = connect_db()
    cur = conn.cursor()
    
    # 主力合约数据（名称带月份，使用独立前缀区分）
    main_contracts = [
        ("豆油2509", "DCE.y2509", 5800.0, 60.0, 100, 1),
        ("铁矿石2509", "DCE.i2509", 7500.0, 70.0, 150, 1),
        ("螺纹钢2410", "SHFE.rb2410", 5000.0, 50.0, 80, 1),
        ("原油2409", "INE.sc2409", 80000.0, 10.0, 200, 1),
        ("沪深3002409", "CFFEX.IF2409", 120000.0, 30.0, 300, 1)
    ]
    
    cur.executemany('''INSERT INTO products(name, exchange_code, margin, stoploss, takeprofit, is_monitored)
                    VALUES(?,?,?,?,?,?)''', main_contracts)
    
    conn.commit()
    print(f"成功新增主力合约记录: {cur.rowcount}条")
    conn.close()
